package com.inossem.warehousestorage_inossem.activity.out_storage;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.adapter.outStorage.PickingOutStorageListAdapter;
import com.inossem.warehousestorage_inossem.bean.outStorage.OutStoragePickingListBean;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.outStorage.OutStoragePickingListRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;

import java.util.ArrayList;
import java.util.List;

//领料出库
public class OutStoragePickingListActivity extends BaseActivity {

    private TextView title;

    private PullToRefreshLayout refresh;

    private PullableListView pickingOutStorageList;

    private List<OutStoragePickingListBean.BodyBean> allData;

    private PickingOutStorageListAdapter adapter;

    private String mCondition;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_out_storage_picking_list);
        initView(OutStoragePickingListActivity.this);
        initData(OutStoragePickingListActivity.this);
    }

    @Override
    protected void onResumeImpl() {

    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        pickingOutStorageList = (PullableListView) activity.findViewById(R.id.pickingOutStorageList);
        refresh = (PullToRefreshLayout) activity.findViewById(R.id.pickingOutStorageListRefresh);
    }

    private void initData(Activity activity) {
        title.setText(getResources().getString(R.string.activity_out_storage_manager_acquisition_of_warehouse));
        ActivityUtils.initBackAndFunction(activity, getResources().getString(R.string.activity_out_storage_sale_outbound), new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });

        ActivityUtils.initRightFunction(activity, ActivityUtils.getRightViewWithName(activity, "获取"), new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                //列表页右上角获取按钮操作
                OutStoragePickingListRequest.request(activity, null, true, new GsonBeanListener<OutStoragePickingListBean>() {
                    @Override
                    public void onSuccess(OutStoragePickingListBean bean) {
                        allData = bean.getBody();
                        if (allData.isEmpty()) {
                            Utils.middleToast(activity, getResources().getString(R.string.activity_in_storage_receive_list_no_message));
                        }
                        addOutStoragePickingListData(activity, ActivityUtils.getShowData(allData, 0), pickingOutStorageList, true);
                    }
                });
            }
        });

        pickingOutStorageList.setPullUpFlag(false);
        pickingOutStorageList.setPullDownFlag(false);
        resetData(activity);
//        setmScanListener((String scanResult, int length) -> {
//
//        }, QRCodeConstant.TYPE_PURCHASE_ORDER);
    }

    private void resetData(Context context) {
        mCondition = "";
        allData = new ArrayList<>();
        addOutStoragePickingListData(context, allData, pickingOutStorageList, true);
    }

    private void addOutStoragePickingListData(Context context, List<OutStoragePickingListBean.BodyBean> pickingOutStorageList, ListView listView, boolean flag) {
        if (flag) {
            adapter = new PickingOutStorageListAdapter(context, pickingOutStorageList, mCondition, (EditText condition) -> {
                NoDoubleClickListener noDoubleClickListener = new NoDoubleClickListener() {
                    @Override
                    public void onNoDoubleClick(View v) {
                        Utils.closeSoftKeyboard(context, v);
                        mCondition = condition.getText().toString();
                        getData(context, mCondition);
                    }
                };
                return noDoubleClickListener;
            });
            listView.setAdapter(adapter);
        }
    }

    private void getData(Context context, String condition) {
        OutStoragePickingListRequest.request(context, condition, false, new GsonBeanListener<OutStoragePickingListBean>() {
            @Override
            public void onSuccess(OutStoragePickingListBean bean) {
                allData = bean.getBody();
                if (allData.isEmpty()) {
                    Utils.middleToast(context, getResources().getString(R.string.activity_in_storage_receive_list_no_message));
                }
                addOutStoragePickingListData(context, ActivityUtils.getShowData(allData, 0), pickingOutStorageList, true);
            }
        });
    }

    public interface OutStoragePickingListener {
        NoDoubleClickListener getNoDoubleClickListener(EditText condition);
    }

}
